<?php


namespace frontend\controllers\cyf;


use frontend\controllers\common;
use frontend\models\Uploade;
use servers\AliPayServer;
use servers\cyf\FaceServer;
use servers\RedisServer;
use yii\web\Controller;
use yii\web\Request;
use yii\web\UploadedFile;

class FaceController extends Controller
{
    /**
     * 人脸检测
     * @throws \Exception
     * @cyf
     */
    public function actionDete(Request $request)
    {
        try {
            if(!$request->isGet) return common::fail('10001','null','请求方式错误');
            $face =new FaceServer();
            $face->facedete();
            $image = 'https://img2.baidu.com/it/u=3354585195,1512541150&fm=253&fmt=auto&app=138&f=JPEG?w=890&h=500';
            $dete = $face->facedete($image,'URL');
            return common::success('0','null','人脸检测成功');
        }catch (\Exception $exception){
            return common::fail('50001','null',$exception->getMessage());
        }
    }


    /**
     * 添加用户组
     * @param Request $request
     * @throws \Exception
     * @cyf
     */
    public function actionAddGrooup(Request $request)
    {
        if(!$request->isPost) return common::fail('10001','null','请求方式错误');
        try {
            $group = $request->post('group_name');
            $face =new FaceServer();
            $res = $face->addGrooup($group);
            return common::success('0','null','添加成功');
        }catch (\Exception $exception){
            return common::fail('50001','null',$exception->getMessage());
        }

    }

    /**
     * 添加人脸
     * @param Request $request
     * @return array|false|string
     * @cyf
     */
    public function actionUserAdd(Request $request)
    {
        if(!$request->isPost) return common::fail('10001','null','请求方式错误');
        try {
            //$file = UploadedFile::getInstanceByName('imageFile');//获取文件
            //if(empty($file)) return common::fail('50002','null','没有识别到人脸');
            //if($file->getHasError()) return common::fail('50002','null','操作失败');
            $image = 'https://img2.baidu.com/it/u=3354585195,1512541150&fm=253&fmt=auto&app=138&f=JPEG?w=890&h=500';

            $face =new FaceServer();
            $res = $face->userAdd($image,'URL','cyf2008a','32');
            return common::success('0','null','添加成功');
        }catch (\Exception $exception){
            return common::fail('50001','null',$exception->getMessage());
        }
    }

    /**
     * 搜索人脸
     * @param Request $request
     * @return array|false|string
     * @cyf
     */
    public function actionSeachFace(Request $request)
    {
        if(!$request->isGet) return common::fail('10001','null','请求方式错误');
        try {
//            $image = 'https://lmg.jj20.com/up/allimg/1114/040221103339/210402103339-8-1200.jpg';
            $image = 'https://img2.baidu.com/it/u=3354585195,1512541150&fm=253&fmt=auto&app=138&f=JPEG?w=890&h=500';
            $face = new FaceServer();
            $res = $face->fachSeach($image,'URL');
        } catch (\Exception $exception){
            return common::fail('50001','null',$exception->getMessage());
        }

    }

}